package cst.a310lhy.recyclelertest310lhy.model;

import java.util.List;

import cst.a310lhy.recyclelertest310lhy.bean.ArticleBean;
import cst.a310lhy.recyclelertest310lhy.iface.ArticleIface;
import cst.a310lhy.recyclelertest310lhy.iface.ArticleListener;
import cst.a310lhy.recyclelertest310lhy.service.ArticleService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2017-05-16.
 */

public class ArticleModel implements ArticleIface {
    private Retrofit retrofit;
    private static final String BASE_URL = "http://amicool.neusoft.edu.cn/";//主机地址

    public ArticleModel() {
        //Retrofit使用-1
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)               //主机地址
                .addConverterFactory(GsonConverterFactory.create()) //解析方式
                .build();
    }


    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener listener) {
        ArticleService service = retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call = service.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("error");
                }
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}